﻿Imports System.Data.Linq
Imports System.Linq
Public Class CMenuController
    Dim objDataContext As New TanNamChinhDataContext
    Public Function SelectAllRecord() As Table(Of tblMenu)
        Try
            Return objDataContext.tblMenus
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectAllRecordByRole(ByVal idRole As Integer) As IQueryable(Of tblMenu)
        Try
            Return (From rm In objDataContext.tblRoleMenus
                    Where rm.ID_Role = idRole
                    Select rm.tblMenu
                    )
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblMenu
        Try
            Return (From r In objDataContext.tblMenus
                    Where r.ID_Menu = _id
                    Select r).Single

        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByName(ByVal _name As String) As tblMenu
        Try
            Return (From r In objDataContext.tblMenus
                    Where r.MenuText = _name
                    Select r).First()

        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblMenu) As Integer
        Try
            objDataContext.tblMenus.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_Menu
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            Dim objUser = (From r In objDataContext.tblMenus
                          Where r.ID_Menu = _id).Single
            objDataContext.tblMenus.DeleteOnSubmit(objUser)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckExists(ByVal sName As String, ByVal id As Integer) As Boolean
        'cach 1
        Return objDataContext.tblMenus.Any(Function(m) m.MenuText.ToLower = sName.ToLower And m.ID_Menu <> id)
    End Function
    
End Class
